package com.leyou.common.advice;

import com.leyou.common.exception.LyException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
@Aspect
public class CommonLogAdvice {
    @Around("within(@org.springframework.stereotype.Service *)|| within(com.baomidou.mybatisplus.extension.service.IService+)")
    public Object handleExceptionLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        try {
            log.debug("{}方法准备调用，参数: {}",proceedingJoinPoint.getSignature(),Arrays.toString(proceedingJoinPoint.getArgs()));
            long l = System.currentTimeMillis();
            Object proceed = proceedingJoinPoint.proceed();
            log.debug("{}方法调用成功，执行耗时{}",proceedingJoinPoint.getSignature(),System.currentTimeMillis()-l);
            return proceed;
        } catch (Throwable throwable) {
            log.error("{}方法执行失败，原因：{}",proceedingJoinPoint.getSignature(),throwable.getMessage(),throwable);
            if (throwable instanceof LyException){
                //如果是ly异常不处理抛
                throw  throwable;
            }
            else{
                //不是就转成ly
                throw new LyException(500,throwable);
            }
        }


    }
}
